// pages/input/input.js
const app = getApp() //!!!
Page({
  data: {
    timename: null,//选择的时间
    numname: null,//选择的题数
    signname: null,//选择的运算模式
    signnumname: null,//选择的运算符数量
    ending:false,//隐藏
    lastresult:false,//结果隐藏
    equation: '', //在这里用全局变量赋值好像不行，所以在onload里赋值
    digital1: 1,
    digital2: 2,
    digital3: 3,
    digital4: 4,
    digital5: 5,
    digital6: 6,
    digital7: 7,
    digital8: 8,
    digital9: 9,
    digital0: 0,
    holder:"?",
    wrongnow:[],
    InputAnswerNumber: '',
    correctnum:0,//做对的题数
    wrongnum:0,//做错的题数
    totalnum:0,
    numpercent:0,//做题进度百分比
    timepercent:0,//时间进度百分比
    correctrate:0,//正确率
    time:0,//定时器变量
    redonumber:0,//重做序号
    playchoice:0,
    modechoice:0,
    gradechoice:0,
    isWujin:false,
    equa:"",
    gamenum1: '',
    gamenum2:'',
    gamenum3:'',
    gamenum4:'',
    gameop1:" ",
    gameop2:" ",
    gameop3:" ",
    holder1:"?",
    holder2:"?",
    holder3:"?",
    input1:'',
    input2:'',
    input3:'',
    choice:0,//当前选择的符号序号
    end1:'=',
    ans0:'',
   ans1:0,
   ans2:0,
   ans3:0

  },
  // 计时器
  setTime() {
    let that = this
    let myTime = setInterval(function () {
      that.setData({
        time: that.data.time + 1,
        timepercent:((that.data.time*100)/(that.data.timename*60)).toFixed(0)
      })
      if (that.data.time > that.data.timename * 60||that.data.ending==true) {
        clearInterval(myTime)
        // 如果时间到了就结束答题
          that.setData({
            time:that.data.time-1,
            ending:true,
            holder:'',
          })
      }
    }, 1000)
  },

  //小游戏模块，随机生成一个四个数的算术表达式，给出算术符号，给出需要用到的4个数，让用户选择将数填入，然后确认判断是否正确
  generateGame1: function(){
    var ans,ans1,ans2;
    var op1,op2,op3,num1,num2,num3,num4;
    var operator = new Array(".", "+", "-", "*", "/");
    do{
    op1 = this.randInt(1,4);
    op2 = this.randInt(1,2);
    //中间符号只有加减
    //op2 = 1;
    op3 = this.randInt(1,4);
    num1 = this.randInt(0,9);
    num2 = this.randInt(1,9);
    num3 = this.randInt(0,9);
    num4 = this.randInt(1,9);
    //如果是加，或者后面的是乘除，可以先算两边
    if(op2==1 || op3==3 || op3==4){
      ans1 = this.SuanGame(num1, operator[op1], num2);
      ans2 = this.SuanGame(num3, operator[op3], num4);
      ans = this.SuanGame(ans1, operator[op2], ans2);
    }
    //如果中间是减，最后也是加减，要先算中间
    else {
      ans1 = this.SuanGame(num1, operator[op1], num2);
      ans2 = this.SuanGame(ans1, operator[op2], num3);
      ans = this.SuanGame(ans2, operator[op3], num4);
    }
    
    }while(!Number.isInteger(ans) || ans < 0 ||!Number.isInteger(ans1) ||!Number.isInteger(ans2) ||ans1<0||ans2<0 );
    
    this.setData({
      choice:0,
      gameop1:operator[op1],
      gameop2:operator[op2],
      gameop3:operator[op3],
      gamenum1:num1.toString(),
      gamenum2:num2.toString(),
      gamenum3:num3.toString(),
      gamenum4:num4.toString(),
      ans0:ans.toString()
    })
    
    
    return 0;
  },

  SuanGame: function (num1, sign1, num2) {
    var ans;
    if (sign1=="+") ans=num1+num2;
    else if (sign1=="-") ans=num1-num2;
    else if (sign1=="*") ans = num1 * num2;
    else if (sign1=="/") ans = num1 / num2;
    return ans;
  },

  //点击得到加号
  inputAnswer_jia: function (e) {
    //console.log(this.data.equation);
    if(this.data.choice==0){
      this.setData({
      input1:"+"
    })
    }
    else if (this.data.choice==1){
      this.setData({
        input2:"+"
      })
    }
    else if (this.data.choice==2){
      this.setData({
        input3:"+"
      })
    }
    
  },

  //点击得到减号
  inputAnswer_jian: function (e) {
    //console.log(this.data.equation);
    if(this.data.choice==0){
      this.setData({
      input1:"-"
    })
    }
    else if (this.data.choice==1){
      this.setData({
        input2:"-"
      })
    }
    else if (this.data.choice==2){
      this.setData({
        input3:"-"
      })
    }
    
  },

  //点击得到乘号
  inputAnswer_cheng: function (e) {
    //console.log(this.data.equation);
    if(this.data.choice==0){
      this.setData({
      input1:"*"
    })
    }
    else if (this.data.choice==1){
      this.setData({
        input2:"*"
      })
    }
    else if (this.data.choice==2){
      this.setData({
        input3:"*"
      })
    }
    
  },

  //点击得到除号
  inputAnswer_chu: function (e) {
    //console.log(this.data.equation);
    if(this.data.choice==0){
      this.setData({
      input1:"/"
    })
    }
    else if (this.data.choice==1){
      this.setData({
        input2:"/"
      })
    }
    else if (this.data.choice==2){
      this.setData({
        input3:"/"
      })
    }
    
  },

  //点击填写下一个
  inputAnswer_next: function (e){
    var k = (this.data.choice + 1)%3;
    this.setData({
      choice: k
    }) 
  },

  //点击清空所有输入
  gameempty: function (e){
    this.setData({
      input1: '',
      input2: '',
      input3: ''
    })
  },
    
  gameconfirm: function (e){
    //答案有空缺
    if (this.data.input1=='' || this.data.input2=='' || this.data.input3==''){
      wx.showToast({
        icon:'none',
        title: '答案未输入完整!',
        duration:1000,
      })
      return;
    }
    var op1 = this.data.input1;
    var op2 = this.data.input2;
    var op3 = this.data.input3;
    var num1 = parseInt(this.data.gamenum1);
    var num2 = parseInt(this.data.gamenum2);
    var num3 = parseInt(this.data.gamenum3);
    var num4 = parseInt(this.data.gamenum4);
    var ans = parseInt(this.data.ans0);
    if(((op1=="+"||op1=="-")&&(op2=="+"||op2=="-")&&(op3=="+"||op3=="-")) || ((op1=="*"||op1=="/")&&(op2=="+"||op2=="-")&&(op3=="+"||op3=="-")) || ((op1=="*"||op1=="/")&&(op2=="*"||op2=="/")&&(op3=="+"||op3=="-")) || ((op1=="*"||op1=="/")&&(op2=="*"||op2=="/")&&(op3=="*"||op3=="/"))){
      this.data.ans1 = this.SuanGame(num1,op1,num2);
      this.data.ans2 = this.SuanGame(this.data.ans1,op2,num3);
      this.data.ans3 = this.SuanGame(this.data.ans2,op3,num4);
    }
    else if((op1=="+"||op1=="-")&&(op2=="+"||op2=="-")&&(op3=="*"||op3=="/")){
      this.data.ans1 = this.SuanGame(num3,op3,num4);
      this.data.ans2 = this.SuanGame(num1,op1,num2);
      this.data.ans3 = this.SuanGame(this.data.ans2,op2,this.data.ans1);
    }
    else if((op1=="+"||op1=="-")&&(op2=="*"||op2=="/")&&(op3=="*"||op3=="/")){
      this.data.ans1 = this.SuanGame(num2,op2,num3);
      this.data.ans2 = this.SuanGame(this.data.ans1,op3,num4);
      this.data.ans3 = this.SuanGame(num1,op1,this.data.ans2);
    }
    else if((op1=="+"||op1=="-")&&(op2=="*"||op2=="/")&&(op3=="+"||op3=="-")){
      this.data.ans1 = this.SuanGame(num2,op2,num3);
      this.data.ans2 = this.SuanGame(num1,op1,this.data.ans1);
      this.data.ans3 = this.SuanGame(this.data.ans2,op3,num4);
    }
    else if((op1=="*"||op1=="/")&&(op2=="+"||op2=="-")&&(op3=="*"||op3=="/")){
      this.data.ans1 = this.SuanGame(num1,op1,num2);
      this.data.ans2 = this.SuanGame(num3,op3,num4);
      this.data.ans3 = this.SuanGame(this.data.ans1,op2,this.data.ans2);
    }

    if(this.data.ans3 == ans){
      wx.showToast({
        title: '回答正确!',
        duration:1000,//显示时长
      })
      this.setData({
        input1: '',
        input2: '',
        input3: ''
      })
      this.data.correctnum=this.data.correctnum+1;
      this.data.numpercent=((this.data.correctnum+this.data.wrongnum)*100/this.data.numname).toFixed(0);
      this.data.correctrate = ((this.data.correctnum)*100/ this.data.numname).toFixed(1);
      //如果题目做完
      if(this.data.correctnum+this.data.wrongnum>=this.data.numname){
        this.setData({
          ending:true,
          holder:'',
          numpercent: this.data.numpercent,
        })
       //  折线图
       // intt是正确率
       var intt=this.data.correctrate;
       var lit = wx.getStorageSync('rateArray');
       if (!lit) {
         lit = [];
       }
       console.log(lit);
       //var lit=app.globalData.rateArray;
       console.log(lit);
       if (lit.length < 8) {
         lit.push(intt);
       }
       else {
         lit.splice(0, 1);
         lit.push(intt);
       }
       //app.globalData.rateArray=lit;
       wx.setStorageSync('rateArray', lit);
       console.log(lit);
     }
     //否则继续下一题
     else{
       this.generateGame1();
       this.data.equation = app.globalData.nowEquation; //全局变量的equation赋值给data里的equation
        this.setData({
          equation: this.data.equation,
          numpercent: this.data.numpercent,
        })
     }
    }
    else{
      this.inputAnswer_end();
        console.log(1)
        return;
    }
 },
/*
  //游戏验证是否正确                 
  gameconfirm: function (e){
    //答案有空缺
    if (this.data.input1=='' || this.data.input2=='' || this.data.input3==''){
      wx.showToast({
        icon:'none',
        title: '答案未输入完整!',
        duration:1000,
      })
      return;
    }
    var op1 = this.data.input1;
    var op2 = this.data.input2;
    var op3 = this.data.input3;
    //检验正确
    if(op1==this.data.gameop1 && op2==this.data.gameop2 && op3==this.data.gameop3){
      wx.showToast({
        title: '回答正确!',
        duration:1000,//显示时长
      })
      this.setData({
        input1: '',
        input2: '',
        input3: ''
      })
      this.data.correctnum=this.data.correctnum+1;
      this.data.numpercent=((this.data.correctnum+this.data.wrongnum)*100/this.data.numname).toFixed(0);
      this.data.correctrate = ((this.data.correctnum)*100/ this.data.numname).toFixed(1);
      //如果题目做完
      if(this.data.correctnum+this.data.wrongnum>=this.data.numname){
        this.setData({
          ending:true,
          holder:'',
          numpercent: this.data.numpercent,
        })
       //  折线图
       // intt是正确率
       var intt=this.data.correctrate;
       var lit = wx.getStorageSync('rateArray');
       if (!lit) {
         lit = [];
       }
       console.log(lit);
       //var lit=app.globalData.rateArray;
       console.log(lit);
       if (lit.length < 8) {
         lit.push(intt);
       }
       else {
         lit.splice(0, 1);
         lit.push(intt);
       }
       //app.globalData.rateArray=lit;
       wx.setStorageSync('rateArray', lit);
       console.log(lit);
     }
     //否则继续下一题
     else{
       this.generateGame1();
       this.data.equation = app.globalData.nowEquation; //全局变量的equation赋值给data里的equation
        this.setData({
          equation: this.data.equation,
          numpercent: this.data.numpercent,
        })
     }


    }
    //回答错误则直接结束
    else{
      this.inputAnswer_end();
        console.log(1)
        return;
    }
},
*/

  //随机生成min-max闭区间内的一个整数
  randInt: function (min, max) {
    var randInt = Math.floor(Math.random() * (max - min + 1)) + min;
    return randInt;
  },
  //若op为1，则返回num1+num2的结果，若op为2，则返回num1-num2的结果
  convertOp: function (op, num1, num2) {
    var ans;
    switch (op) {
      case 1:
        ans = num1 + num2;
        break;
      case 2:
        ans = num1 - num2;
        break;
      case 3:
        ans = num1 * num2;
        break;
      case 4:
        if(num2==0) ans=-1;
        else ans = num1 / num2;
        break;
    }
    return ans;
  },
  convertOp2: function (num1, op1, num2 , op2,num3) {
    var ans;
    if(op1==3||op1==4||op2==1||op2==2){
      ans=this.convertOp(op1,num1,num2);
      if(ans==-1) return ans;
      else ans=this.convertOp(op2,ans,num3);
    }
    else{
      ans=this.convertOp(op2,num2,num3);
      if(ans==-1) return ans;
      else ans=this.convertOp(op1,num1,ans);
    }
    return ans;
  },

  // 重做错题
  generateEquation100: function () {
    var lit = wx.getStorageSync('wrongArray');
    this.setData({
      equa: lit[this.data.redonumber].equation,
      answer: (lit[this.data.redonumber].answer).toString()
    })
    app.globalData.nowEquation = lit[this.data.redonumber].equation;
    app.globalData.nowAnswer = lit[this.data.redonumber].answer;
    return 0;
  },
  ///////////////////////////////////////////////////////////////////////////

  // 删除
  inputAnswer_delete: function (e) {
    var InputAnswerNumber = this.data.InputAnswerNumber;
    var InputAnswerNumberDelete = InputAnswerNumber.split('');
    var NewInputAnswerNumber = InputAnswerNumberDelete.join('').slice(0, -1)
    this.setData({
      InputAnswerNumber: NewInputAnswerNumber
    })
  },
  // 清空
  inputAnswer_empty: function (e) {
    this.setData({
      InputAnswerNumber: '',
    })
  },
  // 点击获取数字
  inputAnswer_digital: function (e) {
    //console.log(this.data.equation);
    var InputAnswerNumber = this.data.InputAnswerNumber;
    this.setData({
      InputAnswerNumber: InputAnswerNumber + e.target.dataset.inputdigit
    })
  },
  //提交答案
  inputAnswer_confirm: function (e) {
    if (this.data.InputAnswerNumber==''){
      wx.showToast({
        icon:'none',
        title: '请输入答案!',
        duration:1000,
      })
      return;
    }
    var input = parseInt(this.data.InputAnswerNumber); //用户的回答转化成整型
    app.globalData.nowInput = input;
    // 统计做题总数
    if (app.globalData.nowplay >= 1 && app.globalData.nowplay<=3)
    {
      var num=wx.getStorageSync('exercisenum');
      if(!num){
        num=1;
      }
      else{
        num=num+1;
      }
      wx.setStorageSync('exercisenum', num);
    }
    // 回答是否正确
    var answer = app.globalData.nowAnswer;
    if (input == answer) {
      wx.showToast({
        title: '回答正确!',
        duration:1000,//显示时长
      })
      this.setData({
        InputAnswerNumber: '',
      })
      this.data.correctnum=this.data.correctnum+1;
      this.data.numpercent=((this.data.correctnum+this.data.wrongnum)*100/this.data.numname).toFixed(0);
      this.data.correctrate = ((this.data.correctnum)*100/ this.data.numname).toFixed(1);
      // 如果题目做完了就答题结束

      if(this.data.correctnum+this.data.wrongnum>=this.data.numname){
         this.setData({
           ending:true,
           holder:'',
           numpercent: this.data.numpercent,
         })
        //  折线图
        // intt是正确率
        var intt=this.data.correctrate;
        var lit = wx.getStorageSync('rateArray');
        if (!lit) {
          lit = [];
        }
        console.log(lit);
        //var lit=app.globalData.rateArray;
        console.log(lit);
        if (lit.length < 8) {
          lit.push(intt);
        }
        else {
          lit.splice(0, 1);
          lit.push(intt);
        }
        //app.globalData.rateArray=lit;
        wx.setStorageSync('rateArray', lit);
        console.log(lit);
      }
      
        //进入下一题
      else{
        if(this.data.playchoice==1){ //基础模式
          this.mode1();
        }
        else if(this.data.playchoice==2){  //强化模式的下一题逻辑
          this.mode2();
        }
        else if(this.data.playchoice==3){
          this.mode3();
        }
        
        this.data.equation = app.globalData.nowEquation; //全局变量的equation赋值给data里的equation
        this.setData({
          equation: this.data.equation,
          numpercent: this.data.numpercent,
        })
      }
    }
    else if (input != answer) {
      if(this.data.playchoice==3){
        this.inputAnswer_end();
        console.log(1)
        return;
      }
      wx.showToast({
        icon:'none',
        duration: 1000,//显示时长
        title: '回答错误!',
      })
      // 错题缓存
        var lit = wx.getStorageSync('wrongArray');
        if (!lit) {
          lit = [];
        }
        var wrong = {
          equation: this.data.equation,
          answer: answer,
          input: input
        }
        this.data.wrongnow.push(wrong);
        this.setData({
          wrongnow: this.data.wrongnow
        })

        lit.push(wrong);
        wx.setStorageSync('wrongArray', lit);

      //缓存错题模块
      var litmod = wx.getStorageSync('wrongmodes');
      if (!litmod) {
        litmod = [];
      }
      if(!litmod.includes(app.globalData.nowmode)){
        var  nowmd = app.globalData.nowmode;
        if (app.globalData.nowmode >= 1 && app.globalData.nowmode <= 11) {
          litmod.push(nowmd);
          wx.setStorageSync('wrongmodes', litmod);
        }
      }  
      var litmmmm = wx.getStorageSync('wrongmodes');
      console.log(litmmmm);
      // 回答错误输入框变为？
      this.setData({
        InputAnswerNumber: '',
      })
      this.data.wrongnum = this.data.wrongnum + 1;
      this.data.numpercent = ((this.data.correctnum + this.data.wrongnum) * 100 / this.data.numname).toFixed(1);
      // 题目做完结束
      if (this.data.correctnum + this.data.wrongnum == this.data.numname) {
        this.setData({
          ending:true,
          holder: '',
          numpercent: this.data.numpercent
        })
        //  折线图
        // intt是正确率
        var intt = this.data.correctrate;
        var lit = wx.getStorageSync('rateArray');
        if (!lit) {
          lit = [];
        }
        //var lit=app.globalData.rateArray;
        console.log(lit);
        if (lit.length < 8) {
          lit.push(intt);
        }
        else {
          lit.splice(0, 1);
          lit.push(intt);
        }
        //app.globalData.rateArray=lit;
        wx.setStorageSync('rateArray', lit);
        console.log(lit);
      }
      // 进入下一题
      else{
        if(this.data.playchoice==1){ //基础模式
          this.mode1();
        }
        else if(this.data.playchoice==2){  //强化模式的下一题逻辑
          this.mode2();
        }
        else {
          this.mode3();
        }
        this.data.equation = app.globalData.nowEquation; //全局变量的equation赋值给data里的equation
        this.setData({
          equation: this.data.equation,
          numpercent: this.data.numpercent,
        })
      }
    }
},
  //提示答题结束
  inputAnswer_end:function(){
    this.setData({
      ending:true,
      InputAnswerNumber: '',
      holder:'',
    })
    console.log(this.data.wrongnow)
  },
  // 显示答题结果
  result:function(){
    this.setData({
      lastresult:!this.data.lastresult,
      correctnum:this.data.correctnum,
      correctrate:this.data.correctrate,
    })
    if(this.data.correctnum>this.data.totalnum){
      this.setData({
        totalnum:this.data.correctnum
      })
    }
    wx.setStorageSync('total', this.data.totalnum);
    // 如果是重做错题，就将还未做的题加入到错题中
    if (app.globalData.nowgrade == 5) {
      var lit = wx.getStorageSync('wrongArray');
      var index = lit.length;
      // 如果没重做完错题
      if (index > this.data.redonumber) {
        var i;
        for (i = this.data.redonumber; i < index; i++) {
          var wrong = {
            equation: lit[i].equation,
            answer: lit[i].answer,
            input: lit[i].input
          }
          this.data.wrongnow.push(wrong);
          this.setData({
            wrongnow: this.data.wrongnow
          })
        }
      }
      var intt = this.data.correctrate;
      var ratelit = wx.getStorageSync('rateArray');
      var num = wx.getStorageSync('exercisenum');
      if (!ratelit) {
        ratelit = [];
      }
      if (ratelit.length < 8) {
        ratelit.push(intt);
      }
      else {
        ratelit.splice(0, 1);
        ratelit.push(intt);
      }
      wx.clearStorage();
      wx.setStorageSync('rateArray', ratelit);
      lit = this.data.wrongnow;
      wx.setStorageSync('wrongArray', lit);
      wx.setStorageSync('exercisenum', num);
    }
  },
  jichuplay:function(){
    if(this.data.modechoice==1){
      this.generateEquation1();
    }
  },


  mode1:function(){
    if(this.data.modechoice==1){
      this.generateEquation1();
    }
    else if(this.data.modechoice==2)
    {
      this.generateEquation2();
    }
    else if (this.data.modechoice == 3){
      this.generateEquation3();
    }
    else if (this.data.modechoice == 4){
      this.generateEquation4();
    }
    else if (this.data.modechoice == 5) {
      this.generateEquation5();
    }
    else if (this.data.modechoice == 6) {
      this.generateEquation6();
    }
    else if (this.data.modechoice == 7) {
      this.generateEquation7();
    }
    else if (this.data.modechoice == 8) {
      this.generateEquation8();
    }
    else if (this.data.modechoice == 9) {
      this.generateEquation9();
    }
    else if (this.data.modechoice == 10) {
      this.generateEquation10();
    }
    else if (this.data.modechoice == 11) {
      this.generateEquation11();
    }
  },
  mode2:function(){
    var op;
    var litmod = wx.getStorageSync('wrongmodes');
    var len=litmod.length;
    var tm=this.randInt(0, len-1);
    if(len==0) {
      op=this.randInt(1,11);
    }
    else {
      op = litmod[tm];
    }
    switch(op){
      case 1:
        this.generateEquation1();
        break;
      case 2:
        this.generateEquation2();
        break;
      case 3:
        this.generateEquation3();
        break;
      case 4:
        this.generateEquation4();
        break;
      case 5:
        this.generateEquation5();
        break;
      case 6:
        this.generateEquation6();
        break;
      case 7:
        this.generateEquation7();
        break;
      case 8:
        this.generateEquation8();
        break;
      case 9:
        this.generateEquation9();
        break;
      case 10:
        this.generateEquation10();
        break;
      case 11:
        this.generateEquation11();
        break;
    }
},
  mode3:function(){
    var op;
    if(this.data.gradechoice==3){
      op=this.randInt(1,11);
    }
    else if(this.data.gradechoice==2){
      op=this.randInt(1,7);
    }
    else{
      op=this.randInt(1,3);
    }
    switch(op){
      case 1:
        this.generateEquation1();
        break;
      case 2:
        this.generateEquation2();
        break;
      case 3:
        this.generateEquation3();
        break;
      case 4:
        this.generateEquation4();
        break;
      case 5:
        this.generateEquation5();
        break;
      case 6:
        this.generateEquation6();
        break;
      case 7:
        this.generateEquation7();
        break;
      case 8:
        this.generateEquation8();
        break;
      case 9:
        this.generateEquation9();
        break;
      case 10:
        this.generateEquation10();
        break;
      case 11:
        this.generateEquation11();
        break;
    }
  },
  onLoad: function (options) {
    this.setTime()//计时器
     let that = this
     app.globalData.nowgrade=wx.getStorageSync('grade');
     this.data.totalnum=wx.getStorageSync('total');
     this.setData({
     modechoice:app.globalData.nowmode,
     gradechoice:app.globalData.nowgrade,
     playchoice:app.globalData.nowplay,
     totalnum:this.data.totalnum
     })
    
      this.setData({
        isWujin:true,
        timename: 100,
      numname: 10000,
      equation: this.data.equation,
      })
    
    //直接生成游戏题目
    this.generateGame1();
    this.data.equation = app.globalData.nowEquation; //全局变量的equation赋值给data里的equation
        this.setData({
          equation: this.data.equation,
          numpercent: this.data.numpercent,
          gamenum1:this.data.gamenum1,
          gamenum2:this.data.gamenum2,
          gamenum3:this.data.gamenum3,
          gamenum4:this.data.gamenum4,
          gameop1:this.data.gameop1,
          gameop2:this.data.gameop2,
          gameop3:this.data.gameop3,
          ans0:this.data.ans0
        })

    
  
 
  },
})